package com.scut.chenlujie.library.clink.sample.client;

import com.scut.chenlujie.library.clink.core.IoContext;
import com.scut.chenlujie.library.clink.impl.IoSelectorProvider;
import com.scut.chenlujie.library.clink.impl.SchedulerImpl;
import com.scut.chenlujie.library.clink.sample.client.bean.ServerInfo;
import com.scut.chenlujie.library.clink.sample.foo.foo;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ClientTest {
    private static boolean done;

    public static void main(String[] args) throws IOException {
        File cachePath = foo.getCacheDir("client/test");
        IoContext.setup().ioProvider(new IoSelectorProvider())
                .scheduler(new SchedulerImpl(1))
                .start();
        ServerInfo info = UDPSearcher.searchServer(10000);
        if (info == null) {
            return;
        }

        int size = 0;
        final List<TCPClient> tcpClients = new ArrayList<>();
        for (int i = 0; i < 200 ; i++) {
            try {
                TCPClient tcpClient = TCPClient.startWith(info, cachePath);
                if (tcpClient == null) {
                    throw new NullPointerException();
                }
                tcpClients.add(tcpClient);
                System.out.println("连接成功：" + (++size));
            } catch (IOException|NullPointerException e) {
                System.out.println("连接异常");
                break;
            }

//            try {
//                Thread.sleep(20);
//            } catch (InterruptedException  e) {
//                e.printStackTrace();
//            }
        }

        System.out.println("start send words");
        //System.in.read();

        Runnable runnable = () -> {
            while (!done) {
                for (TCPClient tcpClient : tcpClients) {
                    tcpClient.send("Hello~~");
                }
                try {
                    Thread.sleep(1500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
        System.in.read();
        done = true;
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (TCPClient tcpClient : tcpClients) {
            tcpClient.exit();
        }
        IoContext.close();
    }
}

